#include "PseudoRNG.h"

PseudoRNG::PseudoRNG(const int newSize)
: size(newSize), index(0)
{
	//size = 624
	state = new int[size];
}

PseudoRNG::~PseudoRNG()
{
	delete[] state;
}

void PseudoRNG::initializeGenerator(const int seed)
{
	state[0] = seed;

	for (int i = 1; i < size; i++)
	{
		state[i] = (1812433253 * (state[i - 1] ^ (state[i-1] >> 30)) + i);
	}
}

void PseudoRNG::generateNumbers()
{
	for (int i = 0; i < size; i++)
	{
		int y = (state[i] & 0x80000000) + (state[(i + 1) % size] & 0x7fffffff);
		state[i] = state[(i + 397) % size] ^ (y >> 1);
		if (y % 2 != 0) // is odd.
			state[i] = state[i] ^ 0x9908b0df;
	}
}

int PseudoRNG::extractNumber()
{
	int y = state[index];
	index = ++index % size;

	y = y ^ (y >> 11);
	y = y ^ ((y << 7) & 0x9d2c5680);
	y = y ^ ((y << 15) & 0xefc60000);
	y = y ^ (y >> 18);

	return y;
}